<template>
  <view class="container">
    <!-- 轮播图 -->
    <swiper
      class="banner"
      circular
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="500"
    >
      <swiper-item
        v-for="(item, index) in banners"
        :key="index"
        @click="handleBannerClick(item)"
      >
        <image :src="item.image_url" mode="aspectFill"></image>
      </swiper-item>
    </swiper>

    <!-- 门店列表 -->
    <view class="store-section">
      <view class="section-header">
        <text class="section-title">门店列表</text>
        <text class="view-all" @click="handleViewAll">全部</text>
      </view>
      <view class="store-list">
        <view
          class="store-item"
          v-for="(store, index) in stores"
          :key="index"
          @click="handleStoreClick(store)"
        >
          <image
            class="store-image"
            :src="store.image_url"
            mode="aspectFill"
          ></image>
          <view class="store-info">
            <view class="store-name">{{ store.name }}</view>
            <view class="store-address">{{ store.address }}</view>
            <view class="store-hours">{{ store.business_hours }}</view>
            <view class="store-distance" v-if="store.distance">
              距离您 {{ store.distance.toFixed(1) }}km
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { fetchBannerList } from '../../src/api/banner'

export default {
  data() {
    return {
      banners: [], // 初始化为空数组
      stores: [
        {
          id: 1,
          name: "总店",
          address: "北京市朝阳区建国路88号",
          phone: "010-88888888",
          business_hours: "09:00-21:00",
          description: "中医馆总店，提供全面的中医诊疗服务",
          image_url: "/static/stores/store1.jpg",
          latitude: 39.9042,
          longitude: 116.4074,
          distance: 1.2,
        },
        {
          id: 2,
          name: "东城分店",
          address: "北京市东城区王府井大街1号",
          phone: "010-66666666",
          business_hours: "09:00-21:00",
          description: "东城区分店，交通便利，环境优雅",
          image_url: "/static/stores/store2.jpg",
          latitude: 39.915,
          longitude: 116.4037,
          distance: 2.5,
        },
        {
          id: 3,
          name: "西城分店",
          address: "北京市西城区西单北大街1号",
          phone: "010-77777777",
          business_hours: "09:00-21:00",
          description: "西城区分店，专业团队，服务周到",
          image_url: "/static/stores/store3.jpg",
          latitude: 39.9139,
          longitude: 116.3917,
          distance: 3.8,
        },
      ],
      currentLocation: {
        latitude: 39.9042,
        longitude: 116.4074,
      },
    };
  },
  onLoad() {
    // 模拟获取当前位置
    this.getCurrentLocation();
    this.getBanners(); // 新增获取轮播图数据
  },
  methods: {
    // 获取当前位置
    getCurrentLocation() {
      // 这里使用模拟的位置数据
      this.currentLocation = {
        latitude: 39.9042,
        longitude: 116.4074,
      };
    },

    // 处理轮播图点击
    handleBannerClick(item) {
      if (item.link) {
        uni.navigateTo({
          url: item.link,
        });
      }
    },

    // 处理查看全部
    handleViewAll() {
      console.log("handleViewAll");
      uni.navigateTo({
        url: "/pages/store/list",
      });
    },

    // 处理门店点击
    handleStoreClick(store) {
      uni.navigateTo({
        url: `/pages/store/detail?id=${store.id}`,
      });
    },
    // 获取轮播图数据
    async getBanners() {
      try {
        const res = await fetchBannerList();
        console.log('获取轮播图数据:', res);
        if (res.code === 200) {
          this.banners = res.data.map(item => ({
            id: item.id,
            image_url: item.image_url,
            link: item.link_url || ''
          }));
          console.log('轮播图数据:', this.banners);
        }
      } catch (error) {
        console.error('获取轮播图失败:', error);
        uni.showToast({
          title: '获取轮播图失败',
          icon: 'none'
        });
      }
    },
  }
}
</script>

<style lang="scss">
.container {
  padding: 20rpx;
}

.banner {
  height: 300rpx;
  margin-bottom: 20rpx;
}

.banner image {
  width: 100%;
  height: 100%;
  border-radius: 20rpx;
}

.store-section {
  margin-top: 20rpx;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
}

.view-all {
  font-size: 28rpx;
  color: #007aff;
}

.store-list {
  margin-top: 20rpx;
}

.store-item {
  display: flex;
  padding: 20rpx;
  background: #fff;
  border-radius: 10rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.store-image {
  width: 200rpx;
  height: 200rpx;
  border-radius: 10rpx;
  margin-right: 20rpx;
}

.store-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.store-name {
  font-size: 28rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.store-address {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.store-hours {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.store-distance {
  font-size: 24rpx;
  color: #007aff;
}
</style>